package week_4;

import tookit.Debug;


public class MyCircularLinkedList<E>
{
	private ListNode<E> tail_ = null;
	
	@Override
	public String toString()
	{
		String s = "";
		
		// If the list is empty.
		if (tail_ == null)
		{
			return "-empty-";
		}
		
		// The tail.
		ListNode<E> currentNode = tail_;
		
		do
		{
			currentNode = currentNode.next_;
			s += " -> " + currentNode.toString();
		}
		while (currentNode != tail_);
		
		return s.substring(4);
	}
	
	@Deprecated
	public void add(E node)
	{
		addLast(node);
	}
	
	public void addLast(E node)
	{
		if (tail_ == null)
		{
			tail_ = new ListNode<E>(node, null);
			tail_.next_ = tail_;
		}
		else
		{
			ListNode<E> listNode = new ListNode<E>(node, tail_.next_);
			tail_.next_ = listNode;
			tail_ = listNode;
		}
	}
	
	public void delete(E node)
	{
		//Debug.log("MyCircularLinkedList.delete(" + node + ")");
		
		if (tail_ != null)
		{
			if (tail_.data_.equals(node) && tail_ == tail_.next_)
			{
				Debug.log("De enige node wordt verwijdert.");
				
				tail_ = null;
			}
			/*
			else if (tail_.data_.equals(node))
			{
				//Debug.log("De tail wordt verwijderd.");
				
				// The head.
				ListNode<E> currentNode = tail_.next_;
				
				// Get the one before the tail.
				while (!currentNode.next_.data_.equals(node))
				{
					currentNode = currentNode.next_;
				}
				
				// Point the next to the first element of the list.
				currentNode.next_ = tail_.next_;
				
				// Previous tail can now be cleaned by garbage collector.
				tail_ = currentNode;
			}*/
			else
			{
				// Start at the tail.
				ListNode<E> currentNode = tail_;
				
				boolean found = true;
				
				// Find the one before the node that needs to be deleted.
				// First next is the head, and check until we are back at the
				// tail.
				while (!currentNode.next_.data_.equals(node))
				{
				//	Debug.log("looped item: " + currentNode.next_);
					
					currentNode = currentNode.next_;
					
					if (currentNode == tail_)
					{
						found = false;
						
						break;
					}
				}
				
				// Found?
				if (found)
				{
				//	Debug.log("found: " + currentNode.next_);
					
					if (currentNode.next_ == tail_)
					{
						tail_ = currentNode;
					}
					
					currentNode.next_ = currentNode.next_.next_;
				}
				else
				{
				//	Debug.log("Not found");
				}
			}
		}
	}
	
	public static void main(String[] args)
	{
		MyCircularLinkedList<Integer> list = new MyCircularLinkedList<Integer>();
		/*list.add(10);
		System.out.println(list.toString());
		list.add(200);
		System.out.println(list.toString());
		list.add(-30);
		System.out.println(list.toString());
		list.add(50);
		System.out.println(list.toString());
		list.add(-40);
		System.out.println(list.toString());
		list.add(20);
		System.out.println(list.toString());
		
		list.delete(50);
		System.out.println(list.toString());
		list.delete(200);
		System.out.println(list.toString());
		list.delete(-40);
		System.out.println(list.toString());
		list.delete(15);
		System.out.println(list.toString());
		*/
		list.add(1);
		System.out.println(list.toString());
		list.add(2);
		System.out.println(list.toString());
		list.add(3);
		System.out.println(list.toString());
		list.add(4);
		System.out.println(list.toString());
		list.add(5);
		System.out.println(list.toString());
		list.delete(5);
		System.out.println(list.toString());
		list.delete(3);
		System.out.println(list.toString());
		list.delete(1);
		System.out.println(list.toString());
		list.delete(1);
		System.out.println(list.toString());
	}
}
